package com.liu.communitysystemend.module.ParkingRecord.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.communitysystemend.exception.CommonException;
import com.liu.communitysystemend.exception.SavaException;
import com.liu.communitysystemend.module.ParkingRecord.domain.ParkingRecord;
import com.liu.communitysystemend.module.ParkingRecord.mapper.ParkingRecordMapper;
import com.liu.communitysystemend.module.ParkingRecord.service.ParkingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
*
*/
@Service
public class ParkingRecordServiceImpl implements ParkingRecordService {
    @Autowired
    private ParkingRecordMapper parkingRecordMapper;

    @Override
    public ParkingRecord getVisitorById(String id) {
        if (id == null) {
            throw new CommonException(501, "参数为空");
        }
        LambdaQueryWrapper<ParkingRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParkingRecord::getId,id);
        ParkingRecord parkingRecord = parkingRecordMapper.selectOne(wrapper);
        return parkingRecord;
    }
    public ParkingRecord selectOneById(String id){
        ParkingRecord parkingRecord=parkingRecordMapper.selectById(id);
        return  parkingRecord;
    }

    @Override
    public ParkingRecord update(ParkingRecord parkingRecord) {
        LambdaQueryWrapper<ParkingRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParkingRecord::getId,parkingRecord.getId());
        int update = parkingRecordMapper.update(parkingRecord, wrapper);
        if (update>0){
            return parkingRecord;
        }
        throw new CommonException(502,"更新失败");
    }

    @Override
    public ParkingRecord add(ParkingRecord parkingRecord) {
        if(parkingRecord ==null){
            throw new SavaException("插入用户失败,菜单的类型实体为空");
        }
        int insert=parkingRecordMapper.insert(parkingRecord);
        if(insert>0){
            return parkingRecord;
        }
        throw new SavaException("插入用户失败");
    }

    @Override
    public Page<ParkingRecord> getPage(ParkingRecord parkingRecord) {
        LambdaQueryWrapper<ParkingRecord> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ParkingRecord::getIsDeleted,0);
        Page<ParkingRecord> page=new Page<>(parkingRecord.getPage().getPage(),parkingRecord.getPage().getSize());
        Page<ParkingRecord> poiPage=(Page<ParkingRecord>) parkingRecordMapper.selectPage(page,wrapper);
        return poiPage;
    }

    @Override
    public Integer deleteById(String id) {
        ParkingRecord parkingRecord = selectOneById(id);
        if(parkingRecord==null){
            throw new CommonException(4004,"找不到id为:"+id+"的数据");
        }
        int i=parkingRecordMapper.deleteById(id);
        return i;
    }

    @Override
    public Integer test() {
        for (int i = 0; i < 20; i++) {
            ParkingRecord parkingRecord = new ParkingRecord();
            parkingRecord.setId("100"+i);
            parkingRecord.setLicense("100"+i);
            parkingRecord.setUserId("100"+i);
            parkingRecord.setEntryTime(new Date());
            parkingRecord.setDepartureTime(new Date((new Date().getTime()+36000000L)));
            parkingRecord.setFee(30.0);
            parkingRecord.setPayStatus(1);
            parkingRecord.setValid(1);
            parkingRecord.setParkingSpaceId("100"+i);
            parkingRecordMapper.insert(parkingRecord);
        }
        return 1;
    }

    @Override
    public ParkingRecord selectbyValid(ParkingRecord record) {
        LambdaQueryWrapper<ParkingRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParkingRecord::getUserId,record.getUserId());
        wrapper.eq(ParkingRecord::getParkingSpaceId,record.getParkingSpaceId());
        wrapper.eq(ParkingRecord::getValid,1);
        wrapper.eq(ParkingRecord::getPayStatus,1);
        wrapper.ge(ParkingRecord::getDepartureTime,new Date());
        ParkingRecord parkingRecord = parkingRecordMapper.selectOne(wrapper);
        return parkingRecord;
    }
}
